﻿using System;

namespace EmbeddedSharp.Core.Function.Flow {
    public class WhileNode : FuncNode {

        public WhileNode()
            : base() {
            base.Value = "while";
        }

        public override object Evaluate() {
            string cond = Parameters[Parameters.Count - 1].Evaluate().ToString();
            string result = string.Empty;

            while (cond.ToLower().Equals("true")) {
                try
                {
                    Parameters[Parameters.Count - 2].Evaluate();
                }
                catch (BreakException ex)
                {
                    break;
                }
                catch (ContinueException ex)
                {
                    cond = Parameters[Parameters.Count - 1].Evaluate().ToString();
                    continue;
                }

                cond = Parameters[Parameters.Count - 1].Evaluate().ToString();
            }

            return null;
        }
    }
}
